﻿
using System;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Media;
namespace YoucaiReader.Helper.Drawing
{
    public class AffineTransformImpl : AffineTransform
    {
        public AffineTransformImpl()
        {
            this.Matrix = System.Windows.Media.Matrix.Identity;
        }

        public AffineTransformImpl(System.Windows.Media.Matrix matrix)
        {
            this.Matrix = matrix;
        }

        public double getScaleX()
        {
            return this.Matrix.M11;
        }

        public double getScaleY()
        {
            return this.Matrix.M22;
        }

        public double getTranslateX()
        {
            return this.Matrix.OffsetX;
        }

        public double getTranslateY()
        {
            return this.Matrix.OffsetY;
        }

        private System.Windows.Media.Matrix Multiply(System.Windows.Media.Matrix m1, System.Windows.Media.Matrix m2)
        {
            System.Windows.Media.Matrix matrix = new System.Windows.Media.Matrix();
            matrix.M11 = ((m1.M11 * m2.M11) + (m1.M12 * m2.M21));
            matrix.M12 = ((m1.M11 * m2.M12) + (m1.M12 * m2.M22));
            matrix.M21 = ((m1.M21 * m2.M11) + (m1.M22 * m2.M21));
            matrix.M22 = ((m1.M21 * m2.M12) + (m1.M22 * m2.M22));
            matrix.OffsetX = (((m1.OffsetX * m2.M11) + (m1.OffsetY * m2.M21)) + m2.OffsetX);
            matrix.OffsetY = (((m1.OffsetX * m2.M12) + (m1.OffsetY * m2.M22)) + m2.OffsetY);
            return matrix;
        }

        public void rotate(double theta)
        {
            double num = Math.Cos(theta);
            double num2 = Math.Sin(theta);
            System.Windows.Media.Matrix matrix = new System.Windows.Media.Matrix(num, num2, -num2, num, 0.0, 0.0);
            if (this.Matrix.IsIdentity)
            {
                this.Matrix = matrix;
            }
            else
            {
                this.Matrix = this.Multiply(matrix, this.Matrix);
            }
        }

        public void scale(double sx, double sy)
        {
            System.Windows.Media.Matrix matrix = new System.Windows.Media.Matrix(sx, 0.0, 0.0, sy, 0.0, 0.0);
            if (this.Matrix.IsIdentity)
            {
                this.Matrix = matrix;
            }
            else
            {
                this.Matrix = this.Multiply(matrix, this.Matrix);
            }
        }

        public Point Transform(Point p)
        {
            return new Point(((this.Matrix.M11 * p.X) + (this.Matrix.M21 * p.Y)) + this.Matrix.OffsetX, ((this.Matrix.M12 * p.X) + (this.Matrix.M22 * p.Y)) + this.Matrix.OffsetY);
        }

        public void translate(double tx, double ty)
        {
            System.Windows.Media.Matrix matrix = new System.Windows.Media.Matrix(1.0, 0.0, 0.0, 1.0, tx, ty);
            if (this.Matrix.IsIdentity)
            {
                this.Matrix = matrix;
            }
            else
            {
                this.Matrix = this.Multiply(matrix, this.Matrix);
            }
        }

        public bool IsIdentity
        {
            get
            {
                return this.Matrix.IsIdentity;
            }
        }

        public System.Windows.Media.Matrix Matrix { get; set; }

        public MatrixTransform Value
        {
            get
            {
                MatrixTransform transform = new MatrixTransform();
                transform.Matrix = (this.Matrix);
                return transform;
            }
        }
    }
}

